package com.test.imagepuzzle.bean;

import com.test.imagepuzzle.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class Draw extends View {
	private Bmp bmp[];
	private Bitmap canvasBitmap = Bitmap.createBitmap(480, 500,
			Config.ARGB_8888);
	private Bmp tempBitmap = null;
	private Canvas canvas = new Canvas(canvasBitmap);
	private float X = 0f;
	private float Y = 0f;

	@SuppressWarnings("unused")
	private float DownX = 0f;
	@SuppressWarnings("unused")
	private float DownY = 0f;

	private Bmp[] pic = new Bmp[4];
	private float CX = 0f;
	private float CY = 0f;
	private boolean Begin = true;
	float rotalC[] = new float[2];
	float rotalP[] = new float[2];
	float rotalP_2[] = new float[2];
	int twoPoint = 0;

	private float preLength = 480.0f;
	private float length = 480.0f;
	private float preCos = 0f;
	private float cos = 0f;
	private boolean bool = true;

	public Draw(Context context) {
		super(context);
		int len;
		len = 4;
		bmp = new Bmp[len];
		{

			for (int i = 0; i < len; i++) {
				// bmp[i] = new Bmp(BitmapFactory.decodeFile(path[i]), i, i *
				// 50f, i * 60f);
				bmp[i] = new Bmp(Bitmap.createScaledBitmap(BitmapFactory
						.decodeStream(getResources().openRawResource(
								R.drawable.aa)), 240, 240, false), i, i * 50f,
						i * 60f);
				bmp[i].width = bmp[i].getPic().getWidth();
				bmp[i].height = bmp[i].getPic().getWidth();
			}
		}
		this.pic = bmp;
		this.canvas.drawColor(-232432445);
		for (int i = 0; i < 4; i++) {
			tempBitmap = pic[0].findByPiority(pic, i);
			tempBitmap.matrix.preTranslate(
					tempBitmap.getXY(1) - tempBitmap.getWidth() / 2,
					tempBitmap.getXY(2) - tempBitmap.getHeight() / 2);
			this.canvas.drawBitmap(tempBitmap.pic, tempBitmap.matrix, null);
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawBitmap(canvasBitmap, 0, 0, null);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// 向上弹起
		if (// event.getAction() == MotionEvent.ACTION_UP
		event.getAction() == MotionEvent.ACTION_POINTER_1_UP
				|| event.getAction() == MotionEvent.ACTION_POINTER_2_UP) {
			bool = true;
			if (twoPoint > 0)
				twoPoint--;
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN
				&& event.getPointerCount() == 1) {
			order(event);
			this.X = event.getX();
			this.Y = event.getY();
			CX = pic[3].findByPiority(pic, 3).getXY(1) - event.getX();
			CY = pic[3].findByPiority(pic, 3).getXY(2) - event.getY();
			Begin = true;
		}

		if (event.getAction() == MotionEvent.ACTION_MOVE && Begin
				&& event.getPointerCount() == 1
		// && twoPoint == 0
		) {
			this.X = event.getX();
			this.Y = event.getY();
			this.canvas.drawColor(-232432445);
			for (int i = 0; i < 3; i++) {
				tempBitmap = pic[0].findByPiority(pic, i);
				// tempBitmap.matrix.preTranslate(0f, 0f);
				canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null);
			}
			tempBitmap = pic[0].findByPiority(pic, 3);
			rotalP = rotalPoint(new float[] { this.X, this.Y },
					tempBitmap.preX, tempBitmap.preY, tempBitmap.width / 2,
					tempBitmap.height / 2, tempBitmap.matrix);
			if ((Math.abs(X - pic[0].findByPiority(pic, 3).getXY(1)) < pic[0]
					.findByPiority(pic, 3).getWidth() / 2)
					&& (Math.abs(Y - pic[0].findByPiority(pic, 3).getXY(2)) < pic[0]
							.findByPiority(pic, 3).getHeight() / 2)) {
				Log.i("the reeal width", String.valueOf(tempBitmap.getWidth()));
				rotalC = this.getT(tempBitmap.width / 2, tempBitmap.height / 2,
						X + CX, Y + CY, tempBitmap.matrix);
				canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null);
				tempBitmap.preX = X + CX;
				tempBitmap.preY = Y + CY;
			} else {
				// tempBitmap.matrix.preTranslate(0f, 0f);
				canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null);
			}
		}
		// 两指移动
		if (event.getPointerCount() >= 2
				&& event.getAction() == MotionEvent.ACTION_MOVE) {
			twoPoint = 2;
			this.canvas.drawColor(-232432445);
			invalidate();
			for (int i = 0; i < 3; i++) {
				tempBitmap = pic[0].findByPiority(pic, i);
				this.canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix,
						null);
			}
			tempBitmap = pic[0].findByPiority(pic, 3);
			rotalP = rotalPoint(new float[] { event.getX(0), event.getY(0) },
					tempBitmap.preX, tempBitmap.preY, tempBitmap.width / 2,
					tempBitmap.height / 2, tempBitmap.matrix);
			rotalP_2 = rotalPoint(new float[] { event.getX(1), event.getY(1) },
					tempBitmap.preX, tempBitmap.preY, tempBitmap.width / 2,
					tempBitmap.height / 2, tempBitmap.matrix);
			if ((Math.abs(rotalP[0] - pic[0].findByPiority(pic, 3).getXY(1)) < pic[0]
					.findByPiority(pic, 3).width / 2)
					&& (Math.abs(rotalP[1]
							- pic[0].findByPiority(pic, 3).getXY(2)) < pic[0]
							.findByPiority(pic, 3).height / 2)
					&& (Math.abs(rotalP_2[0]
							- pic[0].findByPiority(pic, 3).getXY(1)) < pic[0]
							.findByPiority(pic, 3).width / 2)
					&& (Math.abs(rotalP_2[1]
							- pic[0].findByPiority(pic, 3).getXY(2)) < pic[0]
							.findByPiority(pic, 3).height / 2) || true) {
				if (bool) {
					preLength = spacing(event);
					preCos = cos(event);
					bool = false;
				}
				// 获取角度和长度
				length = spacing(event);
				cos = cos(event);
				// 放大和缩小
				if (length - preLength != 0) {
					tempBitmap.width *= (1.0f + (length - preLength) / length);
					tempBitmap.height *= (1.0f + (length - preLength) / length);
					tempBitmap.pic = Bitmap.createScaledBitmap(BitmapFactory
							.decodeStream(getResources().openRawResource(
									R.drawable.aa)), (int) tempBitmap.width,
							(int) tempBitmap.height, false);
					Log.i("new Width", String.valueOf(pic[0].findByPiority(pic,
							3).getWidth()));
					scale(tempBitmap.width / 2, tempBitmap.height / 2,
							tempBitmap.preX, tempBitmap.preY, tempBitmap.matrix);
				}

				// 旋转
				if (Math.abs(cos) > 3 && Math.abs(cos) < 177
						&& Math.abs(cos - preCos) < 15) {
					tempBitmap.matrix.postRotate(cos - preCos);
					this.getT(tempBitmap.width / 2, tempBitmap.height / 2,
							tempBitmap.preX, tempBitmap.preY, tempBitmap.matrix);
				}
				preCos = cos;
				preLength = length;
				// this.getT(tempBitmap.width / 2, tempBitmap.height / 2 ,
				// tempBitmap.preX, tempBitmap.preY, tempBitmap.matrix);

			}
			// 初始位置
			canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null);
			Log.i("preX and preY", String.valueOf(tempBitmap.preX) + " : "
					+ String.valueOf(tempBitmap.preY));

		}

		if (event.getAction() == MotionEvent.ACTION_UP) {
			CX = 0f;
			CY = 0f;
			Begin = false;
		}
		invalidate();
		Log.i("preX and preY",
				String.valueOf(tempBitmap.preX) + " : "
						+ String.valueOf(tempBitmap.preY));
		return true;
	}

	public void order(MotionEvent event) {
		Bmp temp = null;
		for (int i = 3; i > -1; i--) {
			rotalP = rotalPoint(new float[] { event.getX(), event.getY() },
					pic[0].findByPiority(pic, i).preX,
					pic[0].findByPiority(pic, i).preY,
					pic[0].findByPiority(pic, i).width / 2,
					pic[0].findByPiority(pic, i).height / 2,
					pic[0].findByPiority(pic, i).matrix);
			if ((Math.abs(pic[0].findByPiority(pic, i).getXY(1) - rotalP[0]) < pic[0]
					.findByPiority(pic, i).width / 2)
					&& (Math.abs(pic[0].findByPiority(pic, i).getXY(2)
							- rotalP[1]) < pic[0].findByPiority(pic, i).height / 2)) {
				temp = pic[0].findByPiority(pic, i);
				for (Bmp bmp : pic) {
					if (bmp.getPriority() > pic[0].findByPiority(pic, i)
							.getPriority()) {
						bmp.priority--;
					}
				}
				// temp.setPiority(3);
				Begin = true;
				return;
			}
		}
	}

	public float[] getT(float preX, float preY, float x, float y, Matrix matrix) {

		return null;
	}

	public void scale(float preX, float preY, float x, float y, Matrix matrix) {

	}

	public void setToO(Matrix matrix) {

	}

	public float[] rotalPoint(float[] p, float X, float Y, float width,
			float height, Matrix matrix) {

		return null;
	}

	// 计算长度
	private float spacing(MotionEvent event) {

		return 0;
	}

	// 计算余弦
	private float cos(MotionEvent event) {

		return 0;
	}

}
